/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gac.gacmod.visual;

import java.awt.Color;
import java.awt.Dimension;
import org.gac.gacmod.model.CompositeStep;
import org.gac.gacmod.model.Step;
import org.netbeans.api.visual.border.BorderFactory;
import org.netbeans.api.visual.widget.Widget;

/**
 *
 * @author Tomek
 */
public class DropAreaWidget extends Widget
{

    private CompositeStep _parentModelStep;
    private Widget _highlightWidget;
    private int _location;

    public DropAreaWidget(BoxScene scene, boolean horizontal,
            CompositeStep parentModelStep, int location)
    {
        super(scene);
        _parentModelStep = parentModelStep;
        _location = location;
        _highlightWidget = new Widget( scene );

        if (horizontal)
        {
            _highlightWidget.setMinimumSize(new Dimension(100, 2));
        } else
        {
            _highlightWidget.setMinimumSize(new Dimension(2, 20));
        }
        _highlightWidget.setOpaque(true);
        _highlightWidget.setBackground(Color.WHITE);
        _highlightWidget.setVisible(true);

        this.addChild(_highlightWidget);
        this.setBorder(BorderFactory.createEmptyBorder(10));
        scene.addDropAreaWidget(this);

    }

    public void Drop( Step step )
    {
        _parentModelStep.AddStep(step, _location);
    }

    public void Drop(StepWidgetBase step)
    {
        if ( step._parent != null && step._parent._modelStep == _parentModelStep)
        {
            _parentModelStep.moveChildTo(step._modelStep, _location);
        } else
        {

            if( !_parentModelStep.CanAddStep(step._modelStep, _location))
                    return;

            Step parentStepSrc = step._modelStep.getParent();
            if (parentStepSrc != null && parentStepSrc instanceof CompositeStep)
            {
                _parentModelStep.getRootStep().suspendEvents();
                ((CompositeStep) parentStepSrc).RemoveStep(step._modelStep);
                _parentModelStep.getRootStep().resumeEvents();
            }
            _parentModelStep.AddStep(step._modelStep, _location);
        }

    }

    @Override
    public String toString()
    {
        return "Drop area: " + _location;
    }
    
//    public void setDropAreaVisible( boolean visible )
//    {
//        if( visible )
//            _highlightWidget.setBackground(Color.WHITE);
//        else
//            _highlightWidget.setBackground(Color.WHITE);
//    }
    
    public void setHighlight(boolean highlight)
    {
        if (highlight)
        {
            _highlightWidget.setBackground(Color.RED);
        } else
        {
            _highlightWidget.setBackground(Color.WHITE);
        }
    }


    
    
}

